package batch.apachemail;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;

import batch.common.report.LogWriter;
import batch.common.util.Conf;



/**
 * �̸��� ��� Ŭ����
 * @author Owner
 * 
 */
public class MailSender {
	
	public MailSender() {
	}
	
	//public static String mail_server = Conf.getConf("mail_server");
	//public static String mail_server_port_string = Conf.getConf("mail_server_port");
	public static String mail_server = "localhost";
	public static int mail_server_port = 25;
	
	/**
	 * ���� ���
	 * @param fromAddr
	 * @param toAddr
	 * @param strSubject
	 * @param strMessage
	 * @return
	 */
	public static boolean send(String fromAddr, String toAddr,
			String strSubject, String strMessage) {
		return send(fromAddr, toAddr, strSubject, strMessage, "html");
	}

	/**
	 * ���� ���
	 * @param fromAddr
	 * @param toAddr
	 * @param strSubject
	 * @param strMessage
	 * @param strType
	 * @return
	 */
	public static boolean send(String fromAddr, String toAddr,
			String strSubject, String strMessage, String strType) {
		return sendMail(mail_server, mail_server_port, fromAddr, toAddr, strSubject,
				strMessage, strType);
	}

	/**
	 * ���� ���
	 * @param fromAddr
	 * @param toAddr
	 * @param strSubject
	 * @param strMessage
	 * @param strType
	 * @param file
	 * @return
	 */
	public static boolean send(String fromAddr, String toAddr,
			String strSubject, String strMessage, String strType,
			AttachFile[] file) {
		return sendMail(mail_server, fromAddr, toAddr,
				strSubject, strMessage, strType, file);
	}

	/**
	 * 2008.05.05 children day  Added 
	 * @param strServerAddr
	 * @param port
	 * @param fromAddr
	 * @param fromName
	 * @param toAddr
	 * @param strSubject
	 * @param strMessage
	 * @param strType
	 * @return 
	 * <p>Date: 2008. 05. 05
	 * <p>Description: 
	 * <p>Important:
	 */
	public static boolean sendMail(String fromAddr,String fromName, String toAddr, String strSubject, String strMessage) {
		try {

			Email email = null;
			email = new HtmlEmail();
			email.setCharset("ISO-8859-1");
			email.setHostName(mail_server);			
			email.setSmtpPort(mail_server_port);			

			email.setFrom(fromAddr, fromName);
			email.addTo(toAddr);
			email.setSubject(strSubject);
			((HtmlEmail) email).setHtmlMsg(strMessage);
			
			email.send();
			LogWriter.setLog("HTML mail send success");

			return true;
			
		} catch (EmailException e) {
			return false;
		}
	}
	
	
	/**
	 * ���� ���
	 * 
	 * @param strHostName
	 * @param port
	 * @param fromAddr
	 *            ������ ��� �����ּ�
	 * @param toAddr
	 *            �t�� ��� �����ּ�
	 * @param strSubject
	 *            ���� f��
	 * @param strMessage
	 *            ���� ����
	 * @param strType
	 * @return
	 */
	public static boolean sendMail(String strServerAddr, int port,
			String fromAddr, String toAddr, String strSubject,
			String strMessage, String strType) {

		try {

			Email email = null;
			if(strType.equals("html")){
				email = new HtmlEmail();
			}else{
				email = new MultiPartEmail();
			}

			email.setCharset("ISO-8859-1");
			email.setHostName(strServerAddr);

			email.setFrom(fromAddr);
			email.addTo(toAddr);
			email.setSubject(strSubject);
			if(strType.equals("html")){
				((HtmlEmail) email).setHtmlMsg(strMessage);
			}else{
				email.setMsg(strMessage);
			}

			// ����; ����մϴ�
			email.send();
			LogWriter.setLog("HTML mail send success");

			return true;
			
		} catch (EmailException e) {
			return false;
		}
	}

	/**
	 * ÷�� ���� ���.
	 * 
	 * @param strServerAddr
	 * @param fromAddr
	 *            ������ ��� �����ּ�
	 * @param toAddr
	 *            �޴� ��� �����ּ�
	 * @param strSubject
	 *            ���� f��
	 * @param strMessage
	 *            ���� ����
	 * @param strType
	 * @param oAttachFile
	 *            ÷�� ����
	 * @return
	 */
	public static boolean sendMail(String strServerAddr, String fromAddr,
			String toAddr, String strSubject, String strMessage,
			String strType, AttachFile[] oAttachFile) {

		try {
			MultiPartEmail email = null ;
			if(strType.equals("html")){
				email = new HtmlEmail();
			}else{
				email = new MultiPartEmail();
			}

			email.setCharset("ISO-8859-1");
			email.setHostName(strServerAddr);

			email.setFrom(fromAddr);
			email.addTo(toAddr);
			email.setSubject(strSubject);			
			if(strType.equals("html")){
				((HtmlEmail) email).setHtmlMsg(strMessage);
			}else{
				email.setMsg(strMessage);
			}

			/*
			 * Attachment �� ���δ�.
			 */
			if (oAttachFile != null) {
				// ÷���� attachment d���� ���մϴ�
				EmailAttachment attachment = new EmailAttachment();

				for (int i = 0; i < oAttachFile.length; i++) {
					attachment.setPath(oAttachFile[i].getFilePath());
					attachment.setDisposition(EmailAttachment.ATTACHMENT);
					attachment.setDescription("");
					attachment.setName(oAttachFile[i].getFileName());

					email.attach(attachment);
				}
			}

			email.send();
			LogWriter.setLog("HTML mail send success");

			return true;
			
		} catch (EmailException e) {
			return false;
		}

	}

}
